package com.shnu.work.interceptor;

import com.shnu.work.util.NewRedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 登录拦截器
 *
 * @author Shinomiya Kaguya
 */
@Slf4j
@SuppressWarnings("all")
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        NewRedisUtils newRedisUtils = NewRedisUtils.getRedisUtil();
        HttpSession session = request.getSession();
        String jSessionId = session.getId();
        log.info("【Interceptor】LoginInterceptor==>jsessionid:{}", jSessionId);
        String userInfo;
        try {
            userInfo = newRedisUtils.get(0, jSessionId);
            if (StringUtils.isBlank(userInfo)) {
                userInfo = newRedisUtils.get(1, jSessionId);
            }
            log.info("【Interceptor】LoginInterceptor==>userInfo:{}", userInfo);
        } catch (Exception e) {
            return false;
        }
        log.info("【Interceptor】LoginInterceptor==>userInfo:{}", userInfo);

        return userInfo != null;
    }
}
